package cn.dapeng.base;

import cn.dapeng.util.SleepingUtil;

import java.util.concurrent.TimeUnit;


/**
 * 当线程调用interrupt方法设置中断标志位时， Thread.sleep方法可能会抛异常 InterruptException
 * 抛出异常后，JVM会重新把中断标志位复位
 *
 * sleep wait都会触发异常
 *
 * 但是对synchronized竞争锁有影响
 */
public class SleepAndInterrupt {

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            try {
                SleepingUtil.sleep(5, TimeUnit.SECONDS);
            } catch (Exception e) {
                System.out.println("thread interrupted");
                System.out.println(Thread.currentThread().isInterrupted());
            }
        });

        t1.start();
        SleepingUtil.sleep(1, TimeUnit.SECONDS);
        t1.interrupt();
    }
}
